package com.xying.controller;

import com.bitsinharmony.recognito.Recognito;
import com.github.pagehelper.PageHelper;
import com.xying.common.ResponseCode;
import com.xying.common.ServerResponse;
import com.xying.dao.AttendanceMapper;
import com.xying.dao.VoiceMapper;
import com.xying.pojo.Attendance;
import com.xying.pojo.Usr;
import com.xying.pojo.Voice;
import com.xying.service.AttendanceService;
import com.xying.util.JsonUtil;
import com.xying.util.MD5Util;
import com.xying.util.PropertiesUtil;
import com.xying.util.ValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 声纹处理相关，用于用户新建声纹等操作
 * Created by Hisen on 2021/4/25.
 */
@Controller
@RequestMapping("/attendance")
@Slf4j
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    /**
     * 获取个人考勤信息
     * @param idNo
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAttendance.do")
    public ServerResponse getAttendance(String idNo, @RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        HttpServletRequest request){
        ServerResponse serverResponse = ValidateUtil.validateNormalLogin(request);
        if (!serverResponse.isSuccess()) {
            return serverResponse;
        }

        serverResponse = attendanceService.getAttendances(idNo, pageNum, pageSize);

        return serverResponse;
    }

    /**
     * 客户端打卡
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "createAttendance.do", method = RequestMethod.POST)
    public ServerResponse createAttendance(String jsonParam){
        ServerResponse serverResponse = ValidateUtil.validate(jsonParam);
        if (!serverResponse.isSuccess()){
            return serverResponse;
        }

        Map<String, String> requestMap = JsonUtil.stringToObj(jsonParam, Map.class);
        String username = requestMap.get("username");
        String password = requestMap.get("password");
        String idNo = requestMap.get("idNo");


        String clientUsername = PropertiesUtil.getProperty("client.username");
        String clientPassword = MD5Util.MD5EncodeUTF8(PropertiesUtil.getProperty("client.password"));

        if (!clientUsername.equals(username) || !clientPassword.equals(password)){
            return ServerResponse.createByErrorMessage("校验不通过");
        }

        return attendanceService.createAttendance(idNo);
    }

    /**
     * 修改考勤状态
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateAttendance.do", method = RequestMethod.POST)
    public ServerResponse updateAttendance(HttpServletRequest request, String idNo, int status, String createTime){
        ServerResponse<Usr> serverResponse = ValidateUtil.validateNormalLogin(request);
        if (!serverResponse.isSuccess()) {
            return serverResponse;
        }

        Usr usr = serverResponse.getData();
        if (!usr.getIdNo().equals(idNo)) {
            return ServerResponse.createByErrorMessage("越权操作，用户信息不一致");
        }

        serverResponse = ValidateUtil.validate(status, createTime);
        if (!serverResponse.isSuccess()) {
            return serverResponse;
        }



        return attendanceService.updateAttendance(idNo, status, createTime);
    }
}
